﻿using System.Collections.Generic;
using NFine.Data;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;

namespace NFine.Repository.Biz
{
    public class UserDetailRepository : RepositoryBase<UserDetailEntity>, IUserDetailRepository
    {
        public int ClearConsumeForBalance()
        {
            return Execute(string.Format("update biz_user_detail set F_UserConsume = 0,F_TeamConsume = 0,F_TeamBonus = 0"));
        }

        /// <summary>
        /// 获取网络拓扑图的最大深度
        /// </summary>
        /// <returns></returns>
        public int getMaxTreeDepth()
        {
            string sql = "SELECT max(F_TreeDepth) AS treeDepth FROM biz_user_detail";
            return GetSingleToInt(sql);
        }

        /// <summary>
        /// 获取结算总销售额
        /// </summary>
        /// <returns></returns>
        public decimal GetAllConsume()
        {
            string sql = "SELECT SUM(F_TeamAllConsume) as AllConsume from biz_user_detail where F_TreeDepth = 1";
            return GetSingleToDecimal(sql);
        }

        public int GetAllLevelCount()
        {
            string sql = "SELECT SUM(F_ManagerLevel) AS countLevel FROM biz_user_detail WHERE F_ManagerLevel > 0";
            return GetSingleToInt(sql);
        }

        /// <summary>
        /// 获取所有五级子会员的数量
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="money"></param>
        /// <returns></returns>
        public int GetLevelFiveChildCount(string userId, decimal money)
        {
            string sql =
                string.Format(
                    "SELECT Count(*) FROM biz_user_detail WHERE F_ParentId = '{0}' AND F_TeamAllConsume >= {1}", userId,
                    money);
            return GetSingleToInt(sql);
        }

        public int UpdateTeamHistoryConsume(string userId, decimal money)
        {
            string sql = string.Format("UPDATE biz_user_detail SET F_TeamAllConsume = F_TeamAllConsume + {1},F_TeamConsume = F_TeamConsume + {1} WHERE F_UserId = '{0}'", userId,money);
            return Execute(sql);
        }

        public int UpdateUserLevel(string userId, int level)
        {
            string sql = string.Format("UPDATE biz_user_detail SET F_UserLevel = {1} WHERE F_UserId = '{0}'", userId, level);
            return Execute(sql);
        }

        public int UpdateManagerLevel(string userId, int level)
        {
            string sql = string.Format("UPDATE biz_user_detail SET F_ManagerLevel = {1} WHERE F_UserId = '{0}'", userId, level);
            return Execute(sql);
        }

        public int GetMemeberCount(string f_UserId)
        {
            int count = 0;
            var list = FindList(t => t.F_ParentId == f_UserId&&t.F_UserLevel!=-1);//用户等级为-1的表示无效用户
            if (list.Count > 0)
            {
                count = list.Count;
                foreach (var a in list)
                {
                    count += GetMemeberCount(a.F_UserId);
                }
            }
            return count;
        }
    }
}